import sys
from PyQt6.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout, QPushButton, QLabel
from PyQt6.QtCore import Qt
from PyQt6.QtWidgets import QSplitter, QListView, QInputDialog
from PyQt6.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout, QPushButton, QLabel, QSplitter, QListView, QInputDialog
from PyQt6.QtCore import QStringListModel

class SharedModelDemo(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("共享模型示例")
        self.resize(600, 400)
        
        # 创建共享数据模型
        self.shared_model = QStringListModel()
        self.shared_model.setStringList(["初始项目1", "初始项目2"])
        
        # 创建两个视图
        self.list_view1 = QListView()
        self.list_view1.setModel(self.shared_model)
        
        self.list_view2 = QListView()
        self.list_view2.setModel(self.shared_model)
        
        # 添加项目按钮
        add_btn = QPushButton("添加项目")
        add_btn.clicked.connect(self.add_item)
        
        # 布局
        splitter = QSplitter(Qt.Orientation.Horizontal)
        splitter.addWidget(self.list_view1)
        splitter.addWidget(self.list_view2)
        
        layout = QVBoxLayout()
        layout.addWidget(splitter)
        layout.addWidget(add_btn)
        
        central_widget = QWidget()
        central_widget.setLayout(layout)
        self.setCentralWidget(central_widget)
    
    def add_item(self):
        text, ok = QInputDialog.getText(self, "添加项目", "请输入项目名称:")
        if ok and text:
            current_list = self.shared_model.stringList()
            current_list.append(text)
            self.shared_model.setStringList(current_list)

app = QApplication(sys.argv)
window = SharedModelDemo()
window.show()
sys.exit(app.exec())